package com.tj.collection.application;


import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.tj.collection.db.dao.ClAgentsDao;
import com.tj.collection.db.domain.Admin;
import com.tj.collection.db.enums.AdminStatusEnum;
import com.tj.collection.db.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @ClassName:
 * @Description: 权限模块Application
 * @Author xianengfei
 * @Create 2017/3/8 0008 下午 2:30
 */
@Component
public class PermissionApplication {
    private static final Log log = LogFactory.getLog(PermissionApplication.class);

    @Autowired
    private AdminService adminService;
    @Autowired
    private ClAgentsDao agentsDao;
//    @Autowired
//    private AllocationService allocationService;

    /**
     * 更新管理员状态
     *
     * @param adminId
     * @param operateAdminId
     * @param adminStatus
     * @return
     */
    public Boolean updateAdminStatus(Long adminId, Long operateAdminId, Integer adminStatus) {
        Admin admin = adminService.getOneAdminByAdmin(adminId);
        Admin operateAdmin = adminService.getOneAdminByAdmin(operateAdminId);
        if (admin == null) {
            throw new ApplicationException("被屏蔽的管理员不存在");
        }
        if (operateAdmin == null) {
            throw new ApplicationException("操作的管理员不存在");
        }
        int status = admin.getAdminStatusEnum().ordinal();
        if (adminStatus == AdminStatusEnum.normal.ordinal()) {
            if (status == AdminStatusEnum.normal.ordinal()) {
                throw new ApplicationException("您本身就是正常状态，无需恢复");
            }
        } else if (adminStatus == AdminStatusEnum.screen.ordinal()) {
            if (status == AdminStatusEnum.normal.ordinal()) {
                //禁用管理员这里不再撤回信审和催收的订单 在各自业务controller里处理
            }
        }
        adminService.updateAdminStatus(adminId, adminStatus);
        return true;
    }

    /**
     * 删除管理员
     *
     * @param adminId
     * @param operateAdminId
     * @return
     */
    public Boolean deleteAdmin(Long adminId, Long operateAdminId) {
        Admin admin = adminService.getOneAdminByAdmin(adminId);
        Admin operateAdmin = adminService.getOneAdminByAdmin(operateAdminId);
        if (admin == null) {
            throw new ApplicationException("被屏蔽的管理员不存在");
        }
        if (operateAdmin == null) {
            throw new ApplicationException("操作的管理员不存在");
        }

//        {
//            //删除催收
//            int agentId = admin.getId().intValue();
//            ClAgentsEntity byAdminId = agentsDao.getByAdminId(agentId);
//            if (byAdminId != null && byAdminId.getDeleted() != null && !byAdminId.getDeleted()) {
//                allocationService.deleteAgent(agentId, null);
//                agentsDao.deleteById(byAdminId.getId().intValue());
//                log.info("清理催收单及人员信息");
//            }
//        }

        adminService.deleteAdmin(adminId);
        return true;
    }


}
